<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main" :style="{height:''}">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          default-expand-all
          :cell-class-name="cellClassName"
          row-key="id"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
          <el-table-column label="操作" align="center" fixed="right" width="120">
            <template slot-scope="scope">
              <div v-if="!scope.row.children" class="operate_template">

                <el-button
                  v-if="scope.row.status==2 || scope.row.status==3"
                  v-hasPermi="['product:startWork:getInfo']"
                  type="text"
                  @click.stop="seeStartWork(scope.row,'1')"
                >报工</el-button>
                <span v-else class="el-button--text button_grey">报工</span>
                <el-button
                  v-hasPermi="['product:startWork:list']"
                  type="text"
                  @click.stop="seeStartWork(scope.row,'2')"
                >查看</el-button>
              <!-- <span v-else class="el-button--text button_grey">查看</span> -->
              </div>
            </template>

          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { listForStartWork, listForReportForWorkByPlan } from '@/api/ProductionManagement/taskSheet'
import { getDictSelects } from '@/api/common/common'
// import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
// import chooseFinishOrder from './chooseFinishOrder.vue'
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '生产开工', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        statusList: ['2', '3']
      },
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        // { prop: 'planProductionCode', label: '计划单号', canOrder: false },
        { prop: 'planProductionCode', label: '生产计划单号', minWidth: '140' },

        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '任务单号', minWidth: '140' },
        { prop: 'taskTypeName', label: '任务类型', canOrder: false },
        { prop: 'materialCode', label: '物料编码' },
        { prop: 'materialName', label: '物料名称', canOrder: false },
        { prop: 'materialModel', label: '规格型号', canOrder: false },
        { prop: 'engineeringProcessName', label: '工序', canOrder: false },
        { prop: 'productNumber', label: '生产数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        // { prop: 'productNumber', label: '返工数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'productByName', label: '责任人', canOrder: false },
        { prop: 'finishDate', label: '完成日期', minWidth: '140' }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'code', label: '任务单号', type: 'input' },
        // { prop: 'planProductionCode', label: '计划单号', type: 'input' },
        { prop: 'materialName', label: '物料名称', type: 'input' },
        { prop: 'engineeringProcessName', label: '工序', type: 'input' },
        { prop: 'statusList', label: '状态', type: 'select-multiple', optionsName: 'statusOptions' },
        { prop: 'taskTypeName', label: '任务类型', type: 'input' },
        { prop: 'planProductionCode', label: '生产计划单号', type: 'input' },
        { prop: 'materialCode', label: '物料编码', type: 'input' },
        { prop: 'materialModel', label: '规格型号', type: 'input' },
        { prop: 'productNumber', label: '生产数量', type: 'input' },
        { prop: 'productByName', label: '责任人', type: 'input' },
        { prop: 'finishDate', label: '完成日期', type: 'time' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [ // 选择组件数据源
        { value: '2', label: '待生产' },
        { value: '3', label: '生产中' },
        { value: '4', label: '已完成' },
        { value: '5', label: '已终止' }
      ],
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '任务单号', key: 'code' },
          { type: 'form', name: '任务类型', key: 'taskTypeName' },
          { type: 'form', name: '计划单号', key: 'planProductCode' },
          { type: 'form', name: '物料编码', key: 'materialCode' },
          { type: 'form', name: '物料名称', key: 'materialName' },
          { type: 'form', name: '规格型号', key: 'materialModel' },
          { type: 'form', name: '工艺流程', key: 'engineeringFlowName' },
          { type: 'form', name: '工序', key: 'engineeringFlowDetailName' },
          { type: 'form', name: '生产数量', key: 'productNumber' },
          { type: 'form', name: '责任人', key: 'chargeName' },
          { type: 'form', name: '完成日期', key: 'completeDate' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          { type: 'form', name: '首件检验', key: 'firstItemInspectionName' }
        ]
      },
      showKey2: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '任务单号', key: 'code' },
          { type: 'form', name: '任务类型', key: 'taskTypeName' },
          { type: 'form', name: '计划单号', key: 'planProductCode' },
          { type: 'form', name: '物料编码', key: 'materialCode' },
          { type: 'form', name: '物料名称', key: 'materialName' },
          { type: 'form', name: '规格型号', key: 'materialModel' },
          { type: 'form', name: '工艺流程', key: 'engineeringFlowName' },
          { type: 'form', name: '工序', key: 'engineeringFlowDetailName' },
          { type: 'form', name: '返工数量', key: 'productNumber' },
          { type: 'form', name: '责任人', key: 'chargeName' },
          { type: 'form', name: '完成日期', key: 'completeDate' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          { type: 'form', name: '首件检验', key: 'firstItemInspectionName' }
        ]
      }
    }
  },
  watch: {
    tableData() {
      this.$nextTick(() => {
        for (const elementNodeListOfElement of document.querySelectorAll('.SaleOrderCodeColumnClass')) {
          if (!elementNodeListOfElement.childNodes[0].childNodes[0].className || elementNodeListOfElement.childNodes[0].childNodes[0].className == 'commonTableColumn') {
            const paddingNode = document.createElement('div')
            paddingNode.className = 'SaleOrderCodeColumnClass_expand'
            paddingNode.innerHTML = ''
            elementNodeListOfElement.childNodes[0].insertBefore(paddingNode, elementNodeListOfElement.childNodes[0].childNodes[0])
          }
        }
      })
    }
  },
  created() {
    this.getDictOptions()
  },
  mounted() {
  },
  methods: {

    cellClassName({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 0) {
        return 'SaleOrderCodeColumnClass'
      }
      if (!row.isNotselected) {
        return 'displayNoneClass'
      }
    },
    seeStartWork(row, button) {
      if (row.relateTaskId) {
        this.$router.push({ path: '/reWork', query: { id: row.id, buttonStatus: button }})
      } else {
        this.$router.push({ path: '/startWork', query: { id: row.id, buttonStatus: button }})
      }
    },
    getDictOptions() {
      getDictSelects('shiftCode').then(res => {
        this.statusOptions2 = res.data
      })
    },
    getExportData(data) {
      return listForReportForWorkByPlan(data)
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    refresh2() {
      this.tableData = []
      setTimeout(() => {
        this.getList()
      }, 50)
    },
    getList() {
      this.loading = true
      this.tableData = []
      listForReportForWorkByPlan({ ...this.pageData }).then(res => {
        res.data.map((item) => {
          if (!item.id) {
            // item.id = Date.now()
            item.id = uuidv4()
          }
        })
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
///deep/ .el-table {
//  padding-left: 20px !important;
//}

/deep/.el-tooltip{
  display: flex;
}
/deep/ .SaleOrderCodeColumnClass_expand{
  display: inline-block;
  width: 20px;
  line-height: 20px;
  height: 20px;
  text-align: center;
  margin-right: 3px;
}
/deep/.el-table .displayNoneClass .cell .el-checkbox__inner{
  display: none
}
</style>
